package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.service.SetMealService;
import com.itheima.health.vo.ReportParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

/**
 * 套餐折线图
 */
@RestController
@RequestMapping("/report")
@Slf4j
public class SetMealzheXianConller {
    @Reference
    private SetMealService setMealService;

    public static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
        ArrayList<String> result = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月

        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(sdf.parse(minDate));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(sdf.parse(maxDate));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        Calendar curr = min;
        while (curr.before(max)) {
            result.add(sdf.format(curr.getTime()));
            curr.add(Calendar.MONTH, 1);
        }
        min = null;max = null;curr = null;
        return result;
    }

    /**
     *
     * @param date 要获取日期的月份
     * @return 日期列表
     * @throws ParseException
     */
    public static List<String> getDayListOfMonth(String date) throws ParseException {
        List<String> list = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(date));
        int year = calendar.get(Calendar.YEAR);//年份
        int month = calendar.get(Calendar.MONTH) + 1;//月份
        int day = calendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String aDate = String.valueOf(year) + "-" + month + "-" + i;
            list.add(aDate);
        }
        return list;
    }
    @GetMapping("/setmealReport")
    public Result getMemberReport(ReportParam param){

        List<String> monthList;
        try {
            if (param.getBeginDate().equals(param.getEndDate())){
                monthList = getDayListOfMonth(param.getBeginDate());
            }else{
                monthList = getMonthBetween(param.getBeginDate(), param.getEndDate());
            }
        } catch (ParseException e) {
            log.error("",e);
            return new Result(false, MessageConst.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
        List<Integer> memberCountList =  setMealService.countByMonths(monthList);
        HashMap<String,Object> resultMap = new HashMap<>();
        resultMap.put("months",monthList);
        resultMap.put("memberCount",memberCountList);
        return new Result(true,MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS,resultMap);
    }


}
